package org.cfd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import java.util.stream.Collectors;
import org.cfd.entity.Products;
import org.cfd.mapper.ProductsMapper;
import org.cfd.service.ProductsService;
import org.springframework.stereotype.Service;

/**
 * @author rjs
 * @description 针对表【products(商品表)】的数据库操作Service实现
 * @createDate 2023-09-06 14:59:18
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products>
    implements ProductsService {
  @Override
  public List<Products> searchByNameOrDescription(String query) {
    QueryWrapper<Products> wrapper = new QueryWrapper<>();
    wrapper.like("name", query).or().like("description", query);
    return list(wrapper);
  }

  @Override
  public List<String> getAllCategories() {
    return baseMapper.selectList(null).stream()
        .map(Products::getCategory)
        .distinct()
        .collect(Collectors.toList());
  }

  @Override
  public List<Products> getProductsByCategory(String category) {
    QueryWrapper<Products> wrapper = new QueryWrapper<>();
    wrapper.eq("category", category);
    return baseMapper.selectList(wrapper);
  }
}
